//
//  RSS_ViewViewController.m
//  RSS View
//
//  Created by Dan Giralte on 10/19/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "RSS_ViewViewController.h"
#import "CJSONDeserializer.h"

@implementation RSS_ViewViewController

@synthesize rssResults;
@synthesize loadRSS;
@synthesize RSSData;

-(IBAction)RSSLoad:(id)sender;
{
	[self parseRemoteString];
}

- (void)parseRemoteString {
	NSString *urlString = @"http://services.mygroupsapp.com/Data/Rss?feedUrl=http://web414.com/rss.xml";
	
	// Create NSURL string from formatted string
	NSURL *url = [NSURL URLWithString:urlString];
	
	// Setup and start async download
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
	NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
	
	[connection release];
	[request release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
	
	
	// Store incoming data into a string
	NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	
	NSLog(@"jsonString = \"%@\"", jsonString);
	
	// Create a dictionary from the JSON string
	NSDictionary *dictionary = [self parseString:jsonString];
	
	[self updateRSSData:dictionary];
	
	[jsonString release];
}

- (NSDictionary *)parseString:(NSString *) jsonString
{
	NSLog(@"jsonString = \"%@\"", jsonString);
	
	// Use TouchJSON to populate the data and dictionary
	NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
	NSError *error = nil;
	NSDictionary *dictionary = [[CJSONDeserializer deserializer] 
										 deserializeAsDictionary:jsonData error:&error];
	
	if (error) {
		NSLog(@"An error occurred while parsing the JSON string: %@", jsonString);
	}
	
	return dictionary;
}

- (void)updateRSSData:(NSDictionary *)dictionary
{
	for (id key in dictionary)
	{
		NSDictionary *rssEntry = [[NSDictionary alloc] initWithDictionary:[dictionary objectForKey:key]]; 
		[RSSData addObject:rssEntry];
	}
	
	[super viewDidLoad];
}

#pragma mark -
#pragma mark Table View Data source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [self.RSSData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
			cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *SimpleTableIdentifer = @"SimpleTableIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifer];
	if (cell==nil)
	{
		cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifer] autorelease];
	}
	NSUInteger row = [indexPath row];
	cell.text = [RSSData objectAtIndex:row];
	return cell;
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[RSSData release];
   [super dealloc];
}

@end
